import { EventSender } from "../flash/EventSender";
import { IProtocol } from "../../Framework/Protocol/IProtocol";
import { RMIConnection } from "./RMIConnection";
import { Session } from "./Session";
import { IRMISessionEvent } from "./IRMISessionEvent";
import { IRMIPrepareCommandHandler } from "./IRMIPrepareCommandHandler";
type int = number;
//class Communicator

export class Communicator extends EventSender {
  constructor(protocol: IProtocol) {
    
    super();this._connecting = false;
    this._connected = false;
    this._connection = new RMIConnection(protocol);
  }

  public connect(): Boolean {

    this._connecting = true;
    try {
      this._connection.session = this.session;
      var array = this.url.split(":");
      if (array.length != 2) {
        return false;
      }
      var ip = array[0];
      var port = parseInt(array[1]);
      this.connection.connect(ip, port);
      return true;
    } catch (ex) {
      this._connecting = false;
      // trace("Communicator:: connect.connect error");
    }
    return false;
  }

  public set connecting(arg1: Boolean) {
    this._connecting = arg1;
    return;
  }

  public get connecting(): Boolean {
    return this._connecting;
  }

  public set connected(arg1: Boolean) {
    this._connected = arg1;
    return;
  }

  public get connected(): Boolean {
    return this._connected;
  }

  public set url(arg1: string) {
    this._url = arg1;
    return;
  }

  public get url(): string {
    return this._url;
  }

  public get connection(): RMIConnection {
    return this._connection;
  }

  public set session(arg1: Session) {
    this._session = arg1;
    return;
  }

  public get session(): Session {
    return this._session;
  }

  public set sessionEvent(evt: IRMISessionEvent) {
    this._sessionEvent = evt;
    return;
  }

  public get sessionEvent(): IRMISessionEvent {
    return this._sessionEvent;
  }

  public set prepareCommandHandler(arg1: IRMIPrepareCommandHandler) {
    this._prepareCommandHandler = arg1;
    return;
  }

  public get prepareCommandHandler(): IRMIPrepareCommandHandler {
    return this._prepareCommandHandler;
  }

  private _connecting: Boolean;

  private _connected: Boolean;

  private _url!: string;

  private _connection: RMIConnection;

  private _session!: Session;

  private _sessionEvent!: IRMISessionEvent;

  private _prepareCommandHandler!: IRMIPrepareCommandHandler;
}
